package com.itheima.controller;

import com.itheima.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;

    //用户列表
    @GetMapping("/manage/users")
    public ResponseEntity findByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
       return userManager.findByPage(pageNum,pageSize);
    }

    //用户详情
    @GetMapping("/manage/users/{id}")
    public ResponseEntity findUserInfoById(@PathVariable("id") Long userId){
        return userManager.findUserInfoById(userId);
    }

    @GetMapping("/manage/messages")
    public ResponseEntity findMovementByCondition4Page(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            @RequestParam(value = "uid",required = false)Long userId,
            @RequestParam(required = false)String state){

        /*
        state 有可能是"",null,"''",0,1,2
         */
        Integer status = null;

        if (state == null || "".equals(state) || "''".equals(state) ) {
            status = null;
        }else{
            status = Integer.parseInt(state);
        }


        return userManager.findMovementByCondition4Page(userId,status,pageNum,pageSize);
    }

    //动态详情
    @GetMapping("/manage/messages/{movementId}")
    public ResponseEntity findMovementById(@PathVariable String movementId){
        return userManager.findMovementById(movementId);
    }

    //动态评论列表
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findMovementComemntByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            @RequestParam("messageID") String movementId){
        return userManager.findMovementComemntByPage(movementId,pageNum,pageSize);
    }
}
